<template>
    <div class="app-auth-org-picker">
        <app-orgsector v-if="!Object.is(type, selectMode)" dropdownClass="auth-org-picker-dropdown"></app-orgsector>
        <div class="app-org-sector" v-else>
            <dropdown @on-click="orgSelect" :transfer="true" class="auth-org-sector-dropdown">
                <div class="org-sector">
                    <Input :value="getSelectedOrgName" placeholder="请选择部门" readonly />
                    <icon class="icon-css" size="18" type="md-arrow-dropdown" v-if="selectedOrgArray.length > 0"></icon>
                </div>
                <dropdown-menu class="auth-org-picker--menu" slot="list" v-if="selectedOrgArray.length > 0">
                    <dropdown-item
                        class="menu-item"
                        :name="item.srforgsectorid"
                        v-for="(item, index) in selectedOrgArray"
                        :key="index"
                    >
                        {{ item.srforgsectorname }}
                    </dropdown-item>
                </dropdown-menu>
            </dropdown>
        </div>
    </div>
</template>
<script lang='ts'>
import { Vue, Component, Inject, Prop } from 'vue-property-decorator';
import { GlobalService } from 'ibiz-service';

@Component({})
export default class AppAuthOrgPicker extends Vue {
    /**
     * 注入加载行为
     *
     * @memberof AppOrgSector
     */
    @Inject('reload')
    reload!: any;

    /**
     * 组织数据
     *
     * @type {any}
     * @memberof AppAuthOrgPicker
     */
    @Prop() public editorInstance!: any;

    /**
     * 输入值
     *
     * @type {*}
     * @memberof AppAuthOrgPicker
     */
    @Prop() public value!: any;

    /**
     * 名称
     *
     * @type {string}
     * @memberof AppAuthOrgPicker
     */
    @Prop() public name!: string;

    /**
     *强调的数据来源模式
     */

    @Prop() public type?: string;

    /**
     * 应用上下文
     *
     */
    @Prop() public context!: any;
    /**
     * 默认数据来源模式
     *
     * @type {string}
     * @memberof AppAuthOrgPicker
     *
     */
    public selectMode: string = 'AUTH_ORGPICK';

    /**
     * 选中组织部门id
     *
     * @type {string}
     * @memberof AppOrgSector
     */
    public selectedOrgId: string = '';

    /**
     * 选中组织部门名称
     *
     * @type {string}
     * @memberof AppOrgSector
     */
    get getSelectedOrgName() {
        if (this.value) {
            const selectedValue = this.selectedOrgArray.find((item: any) => {
                return item.srforgsectorid == this.value;
            });
            return selectedValue?.srforgsectorname;
        }
    }

    /**
     * 组织部门名称数组
     *
     * @type {Array<any>}
     * @memberof AppOrgSector
     */
    public selectedOrgArray: Array<any> = [];

    /**
     * 组件初始化数据，vue生命周期
     *
     * @memberof AppOrgSector
     */
    public created() {
        //暂时写死的获取远程数据的request
        this.getOrgData();
    }
    /**
     * 选择组织部门回调
     *
     * @memberof AppOrgSector
     */
    public orgSelect(data: string) {
        if (Object.is(data, this.selectedOrgId)) {
            return;
        }
        let item: any = this.selectedOrgArray.find((_item: any) => Object.is(_item.srforgsectorid, data));
        this.$emit('valueChange', { name: this.name, value: item.srforgsectorid });
    }

    /**
     * 获取数据
     *
     * @memberof AppOrgSector
     */
    public async getOrgData() {
        const service = await new GlobalService().getService('DynamicEntity', this.context);
        if (service && service['getOrgData'] && service['getOrgData'] instanceof Function) {
            service['getOrgData']().then((res: any) => {
                if (res && res.ok && res.data) {
                    this.selectedOrgArray = res.data;
                }
            });
        }
    }
}
</script>
<style lang="less">
@import './app-auth-org-picker.less';
</style>